package com.gimi.cloud.bbp.manage.service.prod.impl;

import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.exception.MsgException;
import com.gimi.cloud.bbp.common.utils.ReflectUtils;
import com.gimi.cloud.bbp.commonService.common.JmOperationLogService;
import com.gimi.cloud.bbp.dao.dao.prod.EcProdCatModelDao;
import com.gimi.cloud.bbp.dao.dao.prod.EcProdCatModelValueDao;
import com.gimi.cloud.bbp.dao.dao.prod.EcProdCategoryDao;
import com.gimi.cloud.bbp.dao.dao.product.ProductSkuDao;
import com.gimi.cloud.bbp.dao.myMapper.core.query.QueryParam;
import com.gimi.cloud.bbp.dto.base.BaseDTO;
import com.gimi.cloud.bbp.dto.prod.EcProdCatModelDTO;
import com.gimi.cloud.bbp.entity.prod.EcProdCatModelEntity;
import com.gimi.cloud.bbp.manage.service.prod.EcProdCatModelService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* 商品模版属性相关接口实现类
* @author loongs
* @Description
* @create 2017-12-18
* @version 1.0
**/
@Service
public class EcProdCatModelServiceImpl implements EcProdCatModelService {
    private EcProdCatModelDao ecProdCatModelDao;
    private EcProdCategoryDao ecProdCategoryDao;
    private EcProdCatModelValueDao ecProdCatModelValueDao;
    private EcProdCatModelValueDao prodCatModelValueDao;
    private ProductSkuDao productSkuDao;
    private JmOperationLogService jmOperationLogService;

    @Autowired
    public EcProdCatModelServiceImpl(EcProdCatModelDao ecProdCatModelDao, EcProdCategoryDao ecProdCategoryDao, EcProdCatModelValueDao ecProdCatModelValueDao, EcProdCatModelValueDao prodCatModelValueDao, ProductSkuDao productSkuDao, JmOperationLogService jmOperationLogService) {
        this.ecProdCatModelDao = ecProdCatModelDao;
        this.ecProdCategoryDao = ecProdCategoryDao;
        this.ecProdCatModelValueDao = ecProdCatModelValueDao;
        this.prodCatModelValueDao = prodCatModelValueDao;
        this.productSkuDao = productSkuDao;
        this.jmOperationLogService = jmOperationLogService;
    }

    @Override
    public List<EcProdCatModelDTO> queryList(EcProdCatModelDTO dto, boolean isPaging) throws Exception {
        if(isPaging){
            PageHelper.startPage(dto.getPageNums(),dto.getPageSizes());
        }
        String sql ="SELECT model.*,cate.prod_cat_name,(SELECT GROUP_CONCAT(val.model_value) FROM ec_prod_cat_model_value val WHERE val.is_delete = 0 AND  val.model_id = model.model_id) all_model_value" +
                " FROM ec_prod_cat_model model" +
                " LEFT JOIN ec_prod_category cate ON cate.prod_cat_id = model.prod_cat_id" +
                " WHERE model.is_delete = 0 AND model.prod_cat_id = :prodCatId ";
        QueryParam queryParam = new QueryParam();
        queryParam.setSql(sql);
        queryParam.setParamObject(dto);
        return ecProdCatModelDao.selectListDTOBySql(queryParam);
    }

    @Override
    public EcProdCatModelDTO load(EcProdCatModelDTO dto, Object id) throws Exception {
        EcProdCatModelEntity prodCatModelEntity = ecProdCatModelDao.queryBy("model_id",id);
        if(null == prodCatModelEntity){
            return null;
        }
        EcProdCatModelDTO prodCatModelDTO = new EcProdCatModelDTO();
        ReflectUtils.copyPropertiesIgnoreNullAndEmptyStr(prodCatModelEntity,prodCatModelDTO);
        return prodCatModelDTO;
    }

    @Override
    public EcProdCatModelDTO saveOrUpdate(EcProdCatModelDTO dto) throws Exception {
        EcProdCatModelEntity prodCatModelEntity = null;
        if(null != dto.getModelId()){
            prodCatModelEntity = ecProdCatModelDao.queryBy("model_id",dto.getModelId());
            if(null == prodCatModelEntity){
                throw new MsgException(ResponseCodeEnum.NO_DATA,"未获取到id为"+dto.getModelId()+"的品类模版，保存失败");
            }
        }else{
            prodCatModelEntity = new EcProdCatModelEntity();
        }
        int count = ecProdCatModelDao.selectCountByCatIdAndPropNameExcludeModelId(dto.getProdCatId(),dto.getProdPropName(),dto.getModelId());
        if(count > 0){
            throw new MsgException(ResponseCodeEnum.PRODCATMODEL_IS_EXIT_WHEN_EDIT,"品类模板‘"+dto.getProdPropName()+"’已经存在，请确认");
        }
        ReflectUtils.copyPropertiesIgnoreNullAndEmptyStr(dto,prodCatModelEntity);
        if(null != dto.getModelId()){
            ecProdCatModelDao.baseUpdateByPrimaryKey(prodCatModelEntity);
        }else{
            ecProdCatModelDao.baseInsert(prodCatModelEntity);
        }
        ReflectUtils.copyPropertiesIgnoreNullAndEmptyStr(prodCatModelEntity,dto);
        return dto;
    }

    @Override
    public int delete(EcProdCatModelDTO dto, Object id, boolean realDel) throws Exception {
        EcProdCatModelEntity prodCatModelEntity= ecProdCatModelDao.queryBy("model_id",id);
        if(prodCatModelEntity == null){
            throw new MsgException(ResponseCodeEnum.NO_DATA,"未获取到id为"+dto.getProdCatId()+"的品类模版，删除失败");
        }
        //判断是否能删除、禁用或者启用、编辑
        EcProdCatModelEntity isPublishedEntity = ecProdCatModelDao.selectProductIsPublished(prodCatModelEntity.getModelId());
        if(isPublishedEntity != null){
            throw new MsgException(ResponseCodeEnum.FAILURE,"删除失败，该品类模板值已上架，请确认后重试！");
        }
        if(!realDel){
            ReflectUtils.copyPropertiesIgnoreNullAndEmptyStr(dto,isPublishedEntity);
            isPublishedEntity.setIsDelete(1);
            return ecProdCatModelDao.baseUpdateByPrimaryKey(isPublishedEntity);
        }
        return ecProdCatModelDao.deleteByPrimaryKey(id);
    }

    @Override
    public int deleteProdCatModelByCatIdExcludeModelIds(BaseDTO dto, String prodCatdId, List<Integer> modelIds) throws Exception {
        String sql ="UPDATE ec_prod_cat_model SET is_delete = 1,update_uid ='"+dto.getUid()+"',update_uname ='"+dto.getUname()+"',update_time=NOW() " +
                "   WHERE is_delete = 0 and prod_cat_id ='"+prodCatdId+"'";
        String modelIdStr = "";
        if(null != modelIds && modelIds.size() > 0){
            for (Integer modelId:modelIds){
                modelIdStr+=modelId+",";
            }
        }
        if(modelIdStr.length()>1){
            modelIdStr = modelIdStr.substring(0,modelIdStr.length()-1);
            sql+=" AND model_id NOT in("+modelIdStr+");";
        }
        QueryParam queryParam = new QueryParam();
        queryParam.setSql(sql);
        return ecProdCatModelDao.updateSql(queryParam);
    }

    @Override
    public List<EcProdCatModelDTO> getProdCatModelByCatId(EcProdCatModelDTO dto, String catId) throws Exception {
        List<EcProdCatModelDTO> prodCatModels = ecProdCatModelDao.queryEcprodCatModelByCatId(catId);
        if(null != prodCatModels && prodCatModels.size() > 0){
            for(EcProdCatModelDTO prodCatmodel:prodCatModels){
                prodCatmodel.setProdCatModelValue(ecProdCatModelValueDao.queryPordCatModelValueByModelId(prodCatmodel.getModelId()));
            }
        }
        return prodCatModels;
    }
}
